﻿Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Collections.Generic

Imports DotNetNuke
Imports DotNetNuke.Security
Imports Hospital
Imports DotNetNuke.Entities.Modules

Namespace DotNetNuke.Modules.Hospital
    Partial Class EditHospital
        Inherits PortalModuleBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim hid As Integer = 0

            If Not Page.IsPostBack Then
                'Register event for delete buton
                Dim strMessage = "Do you sure delete this item?"
                btnDelete.Attributes.Add("onclick", String.Format("return confirm('{0}')", strMessage))
                If Not Request.Params("hid") Is Nothing Then
                    hid = Integer.Parse(Request.Params("hid"))
                    btnDelete.Visible = True
                    btnAddNew.Text = "Update"
                    ltrHeading.Text = "Update a hospital"
                    'Load data info
                    LoadDataInfo(hid)
                Else
                    btnDelete.Visible = False
                    btnAddNew.Text = "Add new"
                End If

            End If

        End Sub

        Private Sub LoadDataInfo(ByVal hid As Integer)
            Dim context As HospitalDataContext = New HospitalDataContext()
            Dim query = (From p In context.cwc_Hospitals Where p.hospitalID = hid _
                         Select New With {p.hospitalID, p.hospitalName}).FirstOrDefault()

            If Not query Is Nothing Then
                txtHospital.Text = query.hospitalName
            End If

        End Sub

        Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Me.Response.Redirect("/cwcare/Manage-Hospital.aspx")
        End Sub

        ''' <summary>
        ''' Delete action
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            Try
                Dim context As HospitalDataContext = New HospitalDataContext()
                Dim hid As Integer = Convert.ToInt32(Request.Params("hid"))
                Dim objectCertify = (From p In context.cwc_Hospitals Where p.hospitalID = hid Select p).FirstOrDefault()

                If Not objectCertify Is Nothing Then
                    context.cwc_Hospitals.DeleteOnSubmit(objectCertify)
                    context.SubmitChanges()
                    Me.Response.Redirect("Default.aspx?TabId=" + Me.TabId.ToString)
                End If
            Catch
                lblMsg.Text = "Has a error when delete this item."
            End Try


        End Sub

        ''' <summary>
        ''' Add action
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Sub btnAddNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
            Try
                Dim context As HospitalDataContext = New HospitalDataContext()
                Dim objectCertify As New cwc_Hospital()
                If Not Request.Params("hid") Is Nothing Then
                    Dim hid As Integer = Convert.ToInt32(Request.Params("hid"))
                    objectCertify = (From p In context.cwc_Hospitals Where p.hospitalID = hid Select p).FirstOrDefault()
                    If Not objectCertify Is Nothing Then
                        If txtHospital.Text.Trim.Length > 255 Then
                            lblMsg.Text = "Max length isn't more greater than 255 characters "
                            divEror.Visible = True
                            Exit Sub
                        Else
                            objectCertify.hospitalName = txtHospital.Text.Trim()
                        End If
                        context.SubmitChanges()
                        lblMsg.Text = "Has updated item successfull."
                        divEror.Visible = True
                    End If

                Else
                    If txtHospital.Text.Trim.Length > 255 Then
                        lblMsg.Text = "Max length isn't more greater than 255 characters "
                        divEror.Visible = True
                        Exit Sub
                    Else
                        objectCertify.hospitalName = txtHospital.Text.Trim()
                    End If
                    context.cwc_Hospitals.InsertOnSubmit(objectCertify)
                    context.SubmitChanges()
                    txtHospital.Text = ""
                    lblMsg.Text = "Has added new item successfull."
                    divEror.Visible = True
                End If
            Catch
                If Not Request.Params("hid") Is Nothing Then
                    lblMsg.Text = "Has a error when update item."
                Else
                    lblMsg.Text = "Has a error when add new item."
                End If
                divEror.Visible = True
            End Try


        End Sub
    End Class

End Namespace
